<?php
namespace HuoShu\Pay;
class spay{
    private $spayversion, $spayurl,$spaymchId,$spaykey,$notify_url;
    
    public function __construct() {
        $this->setConfig();
    }
    
    public function setConfig(){
       
        // 包含配置文件
        $conffile = SITE_PATH . "conf/pay/spay/pcconfig.php";
        if (file_exists($conffile)) {
            $spayconf = include $conffile;
        } else {
            $spayconf = array();
        }
        
        $this->spayversion = $spayconf["version"]; // 汇付宝商户号
        $this->spayurl = $spayconf["url"]; // 汇付宝签名
        $this->spaymchId = $spayconf["mchId"]; // 汇付宝签名
        $this->spaykey = $spayconf["key"]; // 汇付宝签名
    
    }
    
    public function set_notify_url($v){
        $this->notify_url=$v;
    }

    public function submit($order_id,$remark,$amount,$ip,$create_time,$obj){
        header("Content-type:text/html;charset=utf-8");
        
        import("Vendor/wftpay/Utils");
        import("Vendor/wftpay/class/RequestHandler");
        import("Vendor/wftpay/class/ClientResponseHandler");
        import("Vendor/wftpay/class/PayHttpClient");

        $resHandler = new \ClientResponseHandler();
        $reqHandler = new \RequestHandler();
        $pay = new \PayHttpClient();
        //导入回调地址
        $reqHandler->setGateUrl($this->spayurl);
        $reqHandler->setKey($this->spaykey);

        $reqHandler->setParameter('out_trade_no',$order_id);
        $reqHandler->setParameter('body',"购买".C('CURRENCY_NAME'));
        $reqHandler->setParameter('attach',$remark);
        $reqHandler->setParameter('total_fee',$amount*100);
        $reqHandler->setParameter('mch_create_ip',$ip);
        $reqHandler->setParameter('time_start',date('YmdHis',$create_time));
        $reqHandler->setParameter('time_expire',date('YmdHis',$create_time+7200));

        $reqHandler->setParameter('service','pay.weixin.native');//接口类型：pay.weixin.native
        $reqHandler->setParameter('mch_id',$this->spaymchId);//必填项，商户号，由威富通分配
        $reqHandler->setParameter('version',$this->spayversion);

        //通知地址，必填项，接收威富通通知的URL，需给绝对路径，255字符内格式如:http://wap.tenpay.com/tenpay.asp
        //$notify_url = 'http://'.$_SERVER['HTTP_HOST'];			//$reqHandler->setParameter('notify_url',$notify_url.'/payInterface/request.php?method=callback');
        $reqHandler->setParameter('notify_url',$this->notify_url);
        
//        echo $this->notify_url;
        $reqHandler->setParameter('nonce_str',mt_rand(time(),time()+rand()));//随机字符串，必填项，不长于 32 位
        $reqHandler->createSign();//创建签名

        $rdata = \Utils::toXml($reqHandler->getAllParameters());//var_dump($rdata);

        $pay->setReqContent($reqHandler->getGateURL(),$rdata);
        if($pay->call()){
            $resHandler->setContent($pay->getResContent());
            $resHandler->setKey($reqHandler->getKey());
            if($resHandler->isTenpaySign()){
                //当返回状态与业务结果都为0时才返回支付二维码，其它结果请查看接口文档
                if($resHandler->getParameter('status') == 0 && $resHandler->getParameter('result_code') == 0){
//                    $obj->assign('code_img_url',$resHandler->getParameter('code_img_url'));
//                    $obj->assign('out_trade_no',$order_id);
//                    $obj->display('pcnativepay');
                    
                    $code_img_url=$resHandler->getParameter('code_img_url');
                    $obj->wxcallback($code_img_url,$order_id);
//                    echo "<img src='$code_img_url' />";
                    
                    exit;
                }else{
                    echo json_encode(array('status'=>500,'msg'=>'Error Code:'.$resHandler->getParameter('err_code').' Error Message:'.$resHandler->getParameter('err_msg')));
                    exit();
                }
            }
            echo json_encode(array('status'=>500,'msg'=>'Error Code:'.$resHandler->getParameter('status').' Error Message:'.$resHandler->getParameter('message')));
        }else{
            echo json_encode(array('status'=>500,'msg'=>'Response Code:'.$pay->getResponseCode().' Error Info:'.$pay->getErrInfo()));
        }
    }
    
    
    function wx_notify($obj){
		import("Vendor/wftpay/Utils");
		import("Vendor/wftpay/class/ClientResponseHandler");
	
		$resHandler = new \ClientResponseHandler();
	
		$xml = file_get_contents('php://input');
	
		$resHandler->setContent($xml);
		$resHandler->setKey($this->spaykey);
		//判断签名结果
		$signResult = $resHandler->isTenpaySign();
	
		if($signResult){
			if($resHandler->getParameter('status') == 0 && $resHandler->getParameter('result_code') == 0){
				//更改订单状态
				$out_trade_no = $resHandler->getParameter('out_trade_no'); //自己生成的订单状态
				$total_fee = $resHandler->getParameter('total_fee') / 100;
				//将订单状态写入数据表中
				$obj->wxpaydb_after($out_trade_no, $total_fee);
				echo 'success';
				exit;
			}else{
				echo 'failure1';
				exit();
			}
		}else{
			echo 'failure2';
		}
	}
    
    
    
}

